<?php
class tbdata_ctl_admin_cats extends desktop_controller{
	public function index(){

		$syn_href = app::get('desktop')->router()->gen_url( array('app'=>'tbdata','ctl'=>'admin_cats','act'=>'sync_props') );
		$syn_product_href = app::get('desktop')->router()->gen_url( array('app'=>'tbdata','ctl'=>'admin_cats','act'=>'sync_products') );

		$this->finder('tbdata_mdl_cats',array(
			'title'=> $this->app->_('产品列表'),
			'actions'=>array(
				array('label' => '同步属性','confirm'=>'确定要同步所选分类的属性吗？','submit'=>$syn_href,'target'=>'refresh'),
				array('label' => '同步产品','confirm'=>'确定要同步所选分类的产品吗？','submit'=>$syn_product_href,'target'=>'refresh'),
			),
			'use_buildin_recycle'=>true,
			'use_buildin_filter'=>true,
		 )
		);
		
	}
	public function sync_props(){
		$this->begin();
		$queue = app::get('base')->model('queue');
		$cat_model = $this->app->model('cats');

		$rows = $cat_model->getList('*',array('id'=>$_POST['id']),0,-1);

		foreach($rows as $row){
			$__filter['cid'] = $row['cid'];
			$data = array(
				'queue_title'=>app::get('tbdata')->_('同步属性'),
				'start_time'=>time(),
				'params'=>array(
					'filter'=>$__filter,
					'queue_time'=>time(),
				),
				'worker'=>'tbdata_queue_propgets.run',
			);
			$queue->insert($data);
			unset($data['queue_id']);
		}

		$this->end(true,'已加入队列');

	}
	public function sync_products(){
		$this->begin();
		$queue = app::get('base')->model('queue');
		$cat_model = $this->app->model('cats');

		$rows = $cat_model->getList('*',array('id'=>$_POST['id']),0,-1);
		foreach($rows as $row){
			$cid['cid'][] = $row['cid'];
		}
		$data = array(
			'queue_title'=>app::get('tbdata')->_('同步商品'),
			'start_time'=>time(),
			'params'=>array(
				'filter'=>$cid,
				'queue_time'=>time(),
			),
			'worker'=>'tbdata_queue_productgets.run',
		);
		$queue->insert($data);
		$this->end(true,'已加入队列');

	}
	function debug(){
		set_time_limit(0);
		$str = <<<EOF
a:2:{s:6:"filter";a:1:{s:3:"cid";s:4:"1512";}s:10:"queue_time";i:1314346244;}
EOF;
$params = unserialize($str);
        $model = app::get('tbdata')->model('cats');
		$sync = kernel::single('tbdata_sync');
        $db = kernel::database();
		$filter = $params['filter'];
		$cid = $filter['cid'];
		$rows = $model->getList('*',array('cid'=>$cid),0,1);
		if($rows[0]['is_parent']=='true'){
			$leaf_cid = $this->get_one_leaf($cid,$params['queue_time']);
		}else{
			$leaf_cid = $rows[0]['cid'];
		}
		$sync->get_props($leaf_cid);
		$model->update(array('props_download_time'=>time()),array('cid'=>$leaf_cid));
		if($this->get_one_leaf($cid,$params['queue_time'])){
			return 1;
		}else{
			return 0;
		}
	}
	function debug2(){
		set_time_limit(0);
		$str = <<<EOF
a:2:{s:6:"filter";a:1:{s:3:"cid";a:1:{i:0;s:8:"50019780";}}s:10:"queue_time";i:1314352238;}
EOF;
$params = unserialize($str);
        $model = app::get('tbdata')->model('cats');
		$sync = kernel::single('tbdata_sync');
        $db = kernel::database();
		$filter = $params['filter'];
		$cid = $filter['cid'];
		$rows = $model->getList('*',array('cid'=>'50019780','products_download_time|sthan'=>$params['queue_time']),0,1);
		if($rows[0]){
			$cid = $rows[0]['cid'];
			$sync->get_products($cid);
			$model->update(array('products_download_time'=>time()),array('cid'=>$cid));
			return 1;
		}else{
			return 0;
		}

	}

	function get_one_leaf($cid,$queue_time){
        $model = app::get('tbdata')->model('cats');
		$rows = $model->getList('*',array('parent_cid'=>$cid,'props_download_time|sthan'=>$queue_time),0,-1);
		foreach($rows as $row){
			if($row['is_parent']=='true'){
				return $this->get_one_leaf($cid,$queue_time);
			}else{
				$return = $row['cid'];
				return $return;
			}
		}
	}
}